package com.cxrj.zhsq.redCommunity.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.cxrj.zhsq.annotation.SystemControllerLog;
import com.cxrj.zhsq.redCommunity.entity.VolunteerEnroll;
import com.cxrj.zhsq.redCommunity.entity.VolunteerRecruit;
import com.cxrj.zhsq.redCommunity.service.VolunteerRecruitService;

/**
 * 志愿者招募
 * 
 * @author wangm
 * @created @2018年5月17日-上午9:30:23
 *
 */
@RestController
public class VolunteerRecruitController {

	@Resource
	private VolunteerRecruitService volunteerRecruitService;

	/**
	 * 查询招募信息列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "selectVolunteerRecruit", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> selectVolunteerRecruit(HttpServletRequest request, HttpServletResponse response) {
		return volunteerRecruitService.selectVolunteerRecruitList(request, response);
	}

	/**
	 * 根据招募id查询详细信息
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "selectVolunteerRecruitById", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> selectVolunteerRecruitById(String vrId, HttpServletRequest request) {
		return volunteerRecruitService.selectVolunteerRecruitById(vrId, null, request);
	}

	/**
	 * 添加招募信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "insertVolunteerRecruit")
	@SystemControllerLog(description = "添加志愿者招募")
	public Map<String, Object> insertVolunteerRecruit(VolunteerRecruit modle, HttpServletRequest request, HttpServletResponse response) {
		try {
			modle.setRecruitEndTime(new SimpleDateFormat("yyyy-MM-dd").parse(modle.getRecruitEndTimeStr()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return volunteerRecruitService.insertVolunteerRecruit(modle, request, response);
	}

	/**
	 * 修改招募信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "updateVolunteerRecruit")
	public Map<String, Object> updateVolunteerRecruit(VolunteerRecruit modle, HttpServletRequest request, HttpServletResponse response) {
//		try {
//			modle.setRecruitEndTime(new SimpleDateFormat("yyyy-MM-dd").parse(modle.getRecruitEndTimeStr()));
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
		return volunteerRecruitService.updateVolunteerRecruit(modle, request, response);
	}

	/**
	 * 查询招志愿者报名息列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "selectVolunteerEnroll", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> selectVolunteerEnroll(HttpServletRequest request, HttpServletResponse response) {
		return volunteerRecruitService.selectVolunteerEnroll(request, response);
	}

	/**
	 * @Author: WangBo
	 * @Description: 导出数据
	 * @Date: 16:11 2018/3/28
	 */
	@SuppressWarnings({ "unchecked", "resource" })
	@RequestMapping("/exportData")
	public Map<String, Object> ExportData(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String[][] titleArr = { { "enrollName", "姓名" }, { "enrollTel", "联系方式" }, { "enrollTimeStr", "申请时间" } };
		Map<String, Object> data = volunteerRecruitService.selectVolunteerEnroll(request, response);
		List<VolunteerEnroll> list = (List<VolunteerEnroll>) data.get("result");
		if (list == null) {
			map.put("msg", "没有报名数据");
			return map;
		}
		if (list.size() == 0) {
			map.put("msg", "没有报名数据");
			return map;
		}

		for (VolunteerEnroll volunteerEnroll : list) {
			volunteerEnroll.setEnrollTimeStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(volunteerEnroll.getEnrollTime()));
		}

		ByteArrayOutputStream os = new ByteArrayOutputStream();
		Class<VolunteerEnroll> clazz = (Class<VolunteerEnroll>) Class.forName("com.cxrj.zhsq.redCommunity.entity.VolunteerEnroll");
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet();
		HSSFRow row1 = sheet.createRow(0);
		// 创建表头
		for (int i = 0; i < titleArr.length; i++) {
			row1.createCell(i).setCellValue(titleArr[i][1]);
		}
		// 创建数据
		for (int i = 0; i < list.size(); i++) {
			HSSFRow row = sheet.createRow(i + 1);
			for (int j = 0; j < titleArr.length; j++) {
				VolunteerEnroll volunteerEnroll = list.get(i);
				Method method = clazz.getMethod("get" + titleArr[j][0].substring(0, 1).toUpperCase() + titleArr[j][0].substring(1));
				row.createCell(j).setCellValue(method.invoke(volunteerEnroll).toString());
			}
		}

		workbook.write(os);

		byte[] content = os.toByteArray();
		InputStream is = new ByteArrayInputStream(content);
		// 设置response参数，可以打开下载页面
		response.reset();

		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
		response.setHeader("Content-Disposition", "attachment;filename=" + new String(("Excel" + new Date().getTime() + ".xls").getBytes(), "iso-8859-1"));
		response.setContentLength(content.length);
		ServletOutputStream outputStream = response.getOutputStream();
		BufferedInputStream bis = new BufferedInputStream(is);
		BufferedOutputStream bos = new BufferedOutputStream(outputStream);
		byte[] buff = new byte[8192];
		int bytesRead;
		while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
			bos.write(buff, 0, bytesRead);

		}
		bis.close();
		bos.close();
		outputStream.flush();
		outputStream.close();
		return map;
	}
}
